/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.dispositivos;

import com.mapping.Modelo;
import com.opensymphony.xwork2.ActionSupport;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
/**
 *
 * @author Andres
 */
@Namespace(value="/")
@Action(value="buscarModelo", results={@Result(name="success",location="/GestionDispositivos/rBuscarModelo.jsp")})
public class BuscarModeloAction extends ActionSupport{
    private String marca;
    private String tipo;
    private List<Modelo> listaModelos;
    
    @Override
    public String execute(){
         String consulta = "select m.IDMODELO,m.IDMARCA,m.MODELO,m.IDTIPO,m.DESCRIPCION from modelo m inner join marca ma on (m.IdMarca=ma.IdMarca) "
                + "where m.idMarca=?1 and m.idTipo=?2";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em =  emf.createEntityManager();  
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        Query query =  em.createNativeQuery(consulta,Modelo.class);
        query.setParameter(1,marca);
        query.setParameter(2,tipo);
        listaModelos = (List<Modelo>) query.getResultList();
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setMarca(String marca) {
        this.marca = marca;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public List<Modelo> getListaModelos() {
        return listaModelos;
    }

    
    
}